package com.mq.rocketmq.ordermsg;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 19239
 * @Description:
 * @Date: 2024/7/3
 * @Version: 1.0
 */
public class NonOrderMsgSender {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("msg-demo1");
        producer.setNamesrvAddr("localhost:9876");
        String topic = "topicDifListener";
        producer.start();

        createOrders().forEach(order -> {
            Message message = new Message(topic, order.toString().getBytes());
            try {
                producer.send(message);
            } catch (MQClientException e) {
                throw new RuntimeException(e);
            } catch (RemotingException e) {
                throw new RuntimeException(e);
            } catch (MQBrokerException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        producer.shutdown();
    }

    /**
     * 模拟多线程创建订单
     *
     * @return
     */
    private static List<Order> createOrders() {
        List<Order> objects = new ArrayList<>();

        Order order1 = new Order("0000001", "创建订单");
//        Order order11 = new Order("0000003", "创建订单");
//        Order order2 = new Order("0000001", "确认订单");
//        Order order22 = new Order("0000003", "确认订单");
//        Order order3 = new Order("0000001", "支付订单");
//        Order order33 = new Order("0000003", "支付订单");
//        Order order4 = new Order("0000001", "完成订单");
//        Order order44 = new Order("0000003", "完成订单");

        objects.add(order1);
//        objects.add(order11);
//        objects.add(order2);
//        objects.add(order22);
//        objects.add(order3);
//        objects.add(order33);
//        objects.add(order4);
//        objects.add(order44);
        return objects;

    }
}
